Imports System.Data.OleDb
Imports RIBS.Code.DBCode
Imports RIBS.Code.GlobalModule1

Public Class UserSecurity

    Public Enum RIBSSecurityAction
        GroupPermission
        AddHistory
        DeleteHistory
        DeleteMainRec
        EditHistory
        EditOtherUserHistory
        DeleteOtherHistory
        EditUsers
        EnterWorkLoadScreen
        ViewData
        SystemSettings
    End Enum

    Function CheckUserSecurity(ByVal UserId As String, ByVal RibsAction As RIBSSecurityAction) As Boolean
        Try
            Dim UserSecurity As String = ""
            Dim Sql As String = "SELECT UserGroup.UserID, GroupPermission.GroupPermission " & _
                                " FROM UserGroup INNER JOIN GroupPermission ON UserGroup.GroupName = GroupPermission.GroupName" & _
                                " WHERE Ucase(UserGroup.UserID) ='" & UserId.ToUpper & "' " & _
                                " AND GroupPermission.GroupPermission = '" & RibsAction.ToString & "'"

            Dim con As New OleDbConnection(gConnectionString)
            Dim cmd As New OleDbCommand(Sql, con)
            Dim Reader As OleDbDataReader
            con.Open()
            Reader = cmd.ExecuteReader(CommandBehavior.SingleRow)
            If Reader.Read Then
                Reader.Close()
                Return True
            Else
                Reader.Close()
                Return False
            End If
        Catch ex As Exception
            Throw ex
        End Try

    End Function

    Function Save(ByVal dt As DataTable) As Boolean
        Try
            Dim ldt As DataTable = dt.GetChanges()
            If Not IsNothing(ldt) Then
                For Each dr As DataRow In ldt.Rows
                    ExecNoError("Delete From UserGroup Where UserID = '" & dr("UserID") & "'")
                    For Each dc As DataColumn In ldt.Columns
                        'Debug.Write(dc.ColumnName & ":" & dr(dc) & " ")
                        If UCase(dc.ColumnName) <> "USERID" Then
                            If Not IsDBNull(dr(dc)) Then
                                If dr(dc) = True Then
                                    ExecNoError("INSERT INTO UserGroup (UserID, GroupName) Values ('" & dr("UserID") & "', '" & dc.ColumnName & "')")
                                End If
                            End If
                        End If
                    Next
                    'Debug.Print("")
                Next
                Beep()
            End If
            Beep()
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Function GetAll() As DataTable
        Try
            Dim dt As New DataTable("Groups")
            Dim dtUsers As New DataTable("Users")
            Dim dtUserGroup As New DataTable("Groups")
            Dim dtAll As New DataTable
            Dim oUser As New dbUser
            Dim Conn As New OleDbConnection(gConnectionString)
            Dim Adapter As New OleDbDataAdapter
            Dim cmd As New OleDbCommand
            Dim Sql As String = "SELECT * from [Groups] Order By GroupName"

            Adapter.SelectCommand = New OleDbCommand(Sql, Conn)
            Adapter.Fill(dt)

            Sql = " SELECT Users.UserID, Groups.GroupName, Groups.GroupName " & _
                                " FROM Groups INNER JOIN (Users INNER JOIN UserGroup ON " & _
                                " Users.UserID = UserGroup.UserID) ON Groups.GroupName = UserGroup.GroupName " & _
                                " GROUP BY Users.UserID, Groups.GroupName, Groups.GroupName ORDER BY Users.UserID"

            Adapter.SelectCommand = New OleDbCommand(Sql, Conn)
            Adapter.Fill(dtUserGroup)
            dtUsers = oUser.GetAllUsers(True)
            dtAll = CreateDataSet(dt)

            For Each dr As DataRow In dtUsers.Rows
                Dim ldr As DataRow
                ldr = dtAll.NewRow()
                ldr("UserId") = UCase(dr("UserID"))
                For Each drUgrp As DataRow In dtUserGroup.Rows
                    If drUgrp("UserID") = dr("UserID") Then
                        ldr(drUgrp("GroupName")) = True
                    End If
                Next
                dtAll.Rows.Add(ldr)
            Next
            Return dtAll

        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Function CreateDataSet(ByVal dt As DataTable) As DataTable
        Try
            Dim ldt As New DataTable("All")
            ldt.Columns.Add("UserID", System.Type.GetType("System.String")).Unique = True
            For Each dr As DataRow In dt.Rows
                ldt.Columns.Add(dr.Item("GroupName"), System.Type.GetType("System.Boolean"))
            Next
            Return ldt
        Catch ex As Exception
            Throw ex
        End Try
    End Function
End Class
